﻿using System.Collections.Concurrent;
using HslCommunication.Core;

namespace PLC.Domain.ScheduledTasks;

public class PLCHelper
{
    /// <summary>
    /// plc连接字典
    /// </summary>
    private static ConcurrentDictionary<string, IReadWriteNet> _plcDic =
        new ConcurrentDictionary<string, IReadWriteNet>();
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ipEndPoint"></param>
    /// <returns></returns>
    public static bool Remove(string ipEndPoint)
    {
        ArgumentException.ThrowIfNullOrEmpty(ipEndPoint);
        return _plcDic.TryRemove(ipEndPoint, out var value);
    }
    /// <summary>
    /// 获取对象(null 就是获取失败)
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public static IReadWriteNet? Get(string? address)
    {
        ArgumentException.ThrowIfNullOrEmpty(address);
        _plcDic.TryGetValue(address, out var value);
        return value;
    }
    /// <summary>
    /// 添加对象
    /// </summary>
    /// <param name="address"></param>
    /// <param name="readWriteNet"></param>
    /// <returns></returns>
    public static bool Add(string address, IReadWriteNet readWriteNet)
    {
        ArgumentNullException.ThrowIfNull(readWriteNet);
        ArgumentException.ThrowIfNullOrEmpty(address);
        return _plcDic.TryAdd(address, readWriteNet);
    }
}